﻿using Datory;
using SSCMS.Dto;
using SSCMS.Repositories;
using SSCMS.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WF.BeOnDuty.BeOnDuty;
using WF.BeOnDuty.Models;

namespace WF.BeOnDuty.Core
{
    public class DutyFieldRepository: IDutyFieldRepository
    {
        private readonly IPathManager _pathManager;
        private readonly ISiteRepository _siteRepository;
        private readonly Repository<Models.DutyField> _repository;
        public DutyFieldRepository(IPathManager pathManager, ISettingsManager settingsManager, ISiteRepository siteRepository)
        {
            _pathManager = pathManager;
            _siteRepository = siteRepository;
            _repository = new Repository<Models.DutyField>(new Database(settingsManager.DatabaseType, settingsManager.DatabaseConnectionString), settingsManager.Redis);
        }

        public async Task<bool> DeleteAsync(int siteId, int id)
        {
            return await _repository.DeleteAsync(id);
        }

        public async Task<List<DutyField>> GetAllAsync(int siteId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(Models.DutyField.SiteId), siteId)
                .OrderBy(nameof(Models.DutyField.Sort))
            );
        }

        public async Task<DutyField> GetAsync(int siteId, int id)
        {
            return await _repository.GetAsync(id);
        }

        public async Task<int> GetCountAsync(int siteId)
        {
            return await _repository.CountAsync(Q.Where(nameof(Models.DutyField.SiteId), siteId));
        }

        public async Task<List<DutyField>> GetPageAsync(int siteId, int page, int perPage = 15)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(Models.DutyField.SiteId), siteId)
                .ForPage(page, perPage)
                .OrderBy(nameof(Models.DutyField.Sort))
            );
        }

        public async Task<int> InsertAsync(DutyField model)
        {
            return await _repository.InsertAsync(model);
        }

        public async Task<bool> UpdateAsync(DutyField model)
        {
            return await _repository.UpdateAsync(model);
        }
    }
}
